<?php

namespace app\api\model;

use think\Model;

class User extends Base
{
    /**
     * 获取用户地址信息
     * @return \think\model\relation\BelongsTo
     */
    public function address()
    {
        return $this->hasOne('UserAddress', 'user_id', 'id');
    }

    /**
     *
     * @param $appId
     * @return array|\PDOStatement|string|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getUserByOpenId($openId)
    {
        return self::where('openid', '=', $openId)
            ->find();
    }

    /**
     * 根据openId创建一个用户
     * @param $openId
     * @return User
     */
    public static function createUserByOpenId($openId)
    {
        return self::create(['openid' => $openId]);
    }
}
